﻿-- Author      : Kurapica

--------------------------
-- PropertySeparator        --
--------------------------
do
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = IGAS.GUI.CreateFrame
    
    local _WidgetName = "PropertySeparator"
    local _Base = IGAS.GUI.Widget.Frame

    local _Widget = IGAS.Studio.Widget
    local _PropertyType = IGAS.GUI.Base.PropertyType

    -- Script
	local nodeHeight = 26
	local nodeIndent = 10
	
	local function FixHeight(self)
		local height = nodeHeight

		if self:GetChild("PropertySet1") then
			if self.__NodeNum > 0 then
				for i = 1, self.__NodeNum do
					if self:GetChild("PropertySet"..i) and self:GetChild("PropertySet"..i).Visible then
						height = height + self:GetChild("PropertySet"..i).Height
					end
				end
			end
		end

		self.Height = height
	end
	
	local function OnSizeChanged(self)
		local height = nodeHeight
		local parent = self.Parent

		if parent:GetChild("PropertySeparator1") then
			if parent.__NodeNum > 0 then
				for i = 1, parent.__NodeNum do
					if parent:GetChild("PropertySeparator"..i) and parent:GetChild("PropertySeparator"..i).Visible then
						height = height + parent:GetChild("PropertySeparator"..i).Height
					end
				end
			end
		end

		parent.Height = height
	end
		
	local function ToggleBtn_OnClick(self)
		self.Parent.ToggleState = not self.Parent.ToggleState
	end
		
    -- ScriptType
    local _ScriptType = {
    }

    --    FuncProxy
    local _FuncProxy = {		
		-- GetToggleState
		["GetToggleState"] = function(self)
			return (self.__ToggleState and true) or false
		end,
		
		-- SetToggleState
		["SetToggleState"] = function(self, flag)
			flag = (flag and true) or false
				
			if self.__ToggleState == flag then
				return
			end

			if flag == false then
				self.__ToggleState = false
				self:GetChild("ToggleBtn"):SetNormalTexture("Interface\\Buttons\\UI-PlusButton-UP")
				self:GetChild("ToggleBtn"):SetPushedTexture("Interface\\Buttons\\UI-PlusButton-DOWN")

				if self.__NodeNum > 0 then
					for i = self.__NodeNum,1,-1 do
						self:GetChild("PropertySet"..i):Hide()
					end
				end
			else
				self.__ToggleState = true
				self:GetChild("ToggleBtn"):SetNormalTexture("Interface\\Buttons\\UI-MinusButton-UP")
				self:GetChild("ToggleBtn"):SetPushedTexture("Interface\\Buttons\\UI-MinusButton-DOWN")

				if self.__NodeNum > 0 then
					local frame
					for i = 1, self.__NodeNum do
						frame = self:GetChild("PropertySet"..i)

						frame:Show()

						-- SetPoint
						if i == 1 then
							frame:SetPoint("TOPRIGHT", self, "TOPRIGHT", 0, - nodeHeight)
							frame:SetPoint("TOPLEFT", self, "TOPLEFT", nodeIndent, - nodeHeight)
						else
							frame:SetPoint("TOPRIGHT", self:GetChild("PropertySet"..(i-1)), "BOTTOMRIGHT", 0, 0)
							frame:SetPoint("TOPLEFT", self:GetChild("PropertySet"..(i-1)), "BOTTOMLEFT", 0, 0)
						end
					end
				end
			end

			FixHeight(self)
		end,	
    }

    --    Property
    local _Property = {		
		-- ToggleState
		["ToggleState"] = {
			["Set"] = function(self, flag)
				self:SetToggleState(flag)
			end,

			["Get"] = function(self)
				return self:GetToggleState()
			end,

			["Type"] = _PropertyType.Boolean,
		},
		-- Text
		["Text"] = {
			["Set"] = function(self, text)
				self:GetChild("TextBtn"):GetChild("Text").Text = text
			end,
			
			["Get"] = function(self)
				return self:GetChild("TextBtn"):GetChild("Text").Text
			end,
			
			["Type"] = _PropertyType.String,
		},
    }

    local function _New(parent)-- Base Frame
        local index = parent.__NodeNum or 0
		index = index + 1

		-- Base Frame
		local frame = CreateFrame("Frame", nil, parent)
		
		-- Init
		frame.__NodeNum = 0
		frame.ID = index
		frame.__ToggleState = true
		frame.Visible = true
		
		frame.Height = nodeHeight
		parent.Height = parent.Height + nodeHeight
		if index == 1 then
			frame:SetPoint("TOPRIGHT", parent, "TOPRIGHT", 0, -6)
			frame:SetPoint("TOPLEFT", parent, "TOPLEFT", 0, -6)
		else
			frame:SetPoint("TOPRIGHT", parent:GetChild("PropertySeparator"..(index-1)), "BOTTOMRIGHT", 0, 0)
			frame:SetPoint("TOPLEFT", parent:GetChild("PropertySeparator"..(index-1)), "BOTTOMLEFT", 0, 0)
		end
		parent.__NodeNum = index

		-- Toggle Button
		local toggleBtn = CreateFrame("Button","ToggleBtn",frame)
		toggleBtn.Width = 14
		toggleBtn.Height = 14
		toggleBtn:SetPoint("TOPLEFT", frame, "TOPLEFT", 4, - 1)

		toggleBtn:SetNormalTexture("Interface\\Buttons\\UI-MinusButton-UP")
		toggleBtn:SetPushedTexture("Interface\\Buttons\\UI-MinusButton-DOWN")
		toggleBtn:SetHighlightTexture("Interface\\Buttons\\UI-PlusButton-Hilight")
		local t = toggleBtn:GetHighlightTexture()
		t:SetBlendMode("ADD")

		-- Text Button
		local textBtn = CreateFrame("Button", "TextBtn", frame)
		textBtn.Height = nodeHeight
		textBtn:SetPoint("RIGHT", frame, "RIGHT", 0, 0)
		textBtn:SetPoint("TOP", frame, "TOP", 0, 0)
		textBtn:SetPoint("LEFT", frame, "LEFT", 14, 0)
		--textBtn:RegisterForClicks("AnyUp")

		t = CreateFrame("FontString", "Text", textBtn, "OVERLAY")
		t:SetFontObject(GameFontNormal)
		t:SetJustifyH("LEFT")
		t:SetAllPoints(textBtn)

		textBtn:SetHighlightTexture("Interface\\QuestFrame\\UI-QuestLogTitleHighlight")
		t = textBtn:GetHighlightTexture()
		t:SetBlendMode("ADD")
		t:SetPoint("TOPLEFT", textBtn, "TOPLEFT", 0, 1)
		t:SetPoint("BOTTOMRIGHT", textBtn, "BOTTOMRIGHT", 0, 1)

		-- scripts
		toggleBtn.OnClick = ToggleBtn_OnClick
		frame:StackScript("OnSizeChanged", OnSizeChanged)

		return frame
    end

    -- Register
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }

    _Widget[_WidgetName] = IGAS:NewWidget(_WidgetInfo)
end
